/**
 *  Contains the classes and interfaces to interact with the EU service.
 *  
 */

package com.thomsonreuters.elektronunified;
/*
 * Exposes the methods and properties a consumer application requires to
 */
/**
 *  Session manager for interaction with the EU service.
 */
public class SessionManager 
{
	/*
	 *  EU consumer manager.
	 */
	private RFAManager rfaSubMan;
	
	/*
	 *  Service availability indicator.
	 */
	boolean sessionStatusUp;
	
	/*
	 *  Service status detail.
	 */		
	String sessionStatusText;
	
	/**
	 * EU service session manager.  
	 * 
	 * Instantiates a session service manager to interact with the EU service.
	 * 
	 *  @param callerApp Reference to the caller.
	 *  @param callerId Client EU identifier as provided by Thomson Reuters.
	 */
    public SessionManager(ClientCallback callerApp, String callerId)
    { 

    	rfaSubMan = new RFAManager(callerApp, callerId);
  
    	sessionStatusText = rfaSubMan.getSessionUpStatusText();
    	sessionStatusUp = rfaSubMan.sessionUpStatus;
   	 
    	return;
    }
    	
	/**
	 * Gets the status detail of the current session.
	 */
	public String getSessionStatusText() {
		return  this.sessionStatusText;
	}
	
	/**
	 * Gets the status of the current session.
	 */
	public boolean isSessionStatusUp() {
		return  this.sessionStatusUp;
	}
 
	/**
	 * Gets the status detail of a sent RequestItem.
	 */
	public String getRequestStatusText() {
		return  rfaSubMan.itemRequestStatusText;
	}
	
	/**
	 * Gets the status of a sent RequestItem.
	 */
	public boolean getRequestStatus() {
		return  rfaSubMan.itemRequestStatusOK;
	}
	
	/**
	 * Sends a created RequestItem to the service
	 * @param itemReq A populated RequestItem.
	 */
    public void sendRequest(RequestItem itemReq)
    {
     	rfaSubMan.sendRequest(itemReq);   	    	
    }
	
    /**
     * Dispatches events returned by the service.
     * @return The number of outstanding events pending, -1 if none..
     */
	public int dispatch()
	{
		int o =	rfaSubMan.runDispatch();
		return o;
	}
	
	/**
	 * Dispatches events returned by the service. 
	 * <P>
	 * If there are no events, waits the specified number of milliseconds.
	 * 
	 * @param w The time to wait in milliseconds.
	 * @return The number of outstanding events pending, -1 if none.
	 */
	public int dispatch(long w)
	{
		int o =	rfaSubMan.runDispatch(w);
		return o;
	}

	/**
	 * Dispatches events returned by the service continuously. 
	 * 
	 */
	public void dispatchLoop()
	{
		rfaSubMan.runDispatchLoop();		
	}

	/**
	 * Closes down the session cleanly. 
	 * 
	 */
	public void clearUp()
	{
		rfaSubMan.cleanUp();
	}

}
